<template>
  <!-- 苹果风格导航栏 -->
  <div id="header" class="apple-header">
    <!-- 主导航栏 -->
    <nav class="main-nav">
      <div class="nav-container">
        <!-- Logo -->
        <div class="nav-logo">
          <router-link to="/" class="logo-link">
            <img src="@/assets/img/logo.png" alt="Logo" />
          </router-link>
        </div>
        
        <!-- 导航菜单 -->
        <ul class="nav-menu">
          <li
            v-for="(item, index) in navList"
            :key="index"
            class="nav-item"
            :class="{ 'active': index == navIndex, 'has-dropdown': item.children.length > 0 }"
            @mouseenter="handleNavHover(index)"
            @mouseleave="handleNavLeave(index)"
          >
            <router-link 
              :to="item.path" 
              class="nav-link"
              @click="navClick(index, item.name)"
            >
              {{ item.name }}
            </router-link>
            
            <!-- 下拉菜单 -->
            <div v-if="item.children.length > 0" class="dropdown-menu">
              <div class="dropdown-content">
                <router-link
                  v-for="(child, childIndex) in item.children"
                  :key="childIndex"
                  :to="child.path"
                  class="dropdown-item"
                  @click="navClick(index, item.name)"
                >
                  <div class="dropdown-item-content">
                    <span class="dropdown-item-title">{{ child.name }}</span>
                    <span class="dropdown-item-desc">{{ getItemDescription(child.path) }}</span>
                  </div>
                </router-link>
              </div>
            </div>
          </li>
        </ul>

        <!-- 联系信息（桌面版） -->
        <div class="nav-contact hidden-xs">
          <div class="contact-info">
            <span class="contact-item">
              <i class="contact-icon">📞</i>
              {{ phone }}
            </span>
          </div>
        </div>

        <!-- 移动端菜单按钮 -->
        <button class="mobile-menu-btn visible-xs" @click="toggleMobileMenu">
          <span class="hamburger-line" :class="{ 'active': mobileMenuOpen }"></span>
          <span class="hamburger-line" :class="{ 'active': mobileMenuOpen }"></span>
          <span class="hamburger-line" :class="{ 'active': mobileMenuOpen }"></span>
        </button>
      </div>
    </nav>

    <!-- 移动端菜单 -->
    <div class="mobile-menu visible-xs" :class="{ 'open': mobileMenuOpen }">
      <div class="mobile-menu-content">
        <div class="mobile-nav-list">
          <div
            v-for="(item, index) in navList"
            :key="index"
            class="mobile-nav-item"
          >
            <router-link 
              :to="item.path" 
              class="mobile-nav-link"
              @click="handleMobileNavClick(index, item.name)"
            >
              {{ item.name }}
            </router-link>
            
            <!-- 移动端子菜单 -->
            <div v-if="item.children.length > 0" class="mobile-sub-menu">
              <router-link
                v-for="(child, childIndex) in item.children"
                :key="childIndex"
                :to="child.path"
                class="mobile-sub-link"
                @click="handleMobileNavClick(index, item.name)"
              >
                {{ child.name }}
              </router-link>
            </div>
          </div>
        </div>
        
        <!-- 移动端联系信息 -->
        <div class="mobile-contact">
          <div class="mobile-contact-item">
            <i class="contact-icon">📞</i>
            <span>{{ phone }}</span>
          </div>
          <div class="mobile-contact-item">
            <i class="contact-icon">✉️</i>
            <span>{{ email }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 背景遮罩 -->
    <div 
      v-if="mobileMenuOpen" 
      class="mobile-menu-overlay"
      @click="closeMobileMenu"
    ></div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'

// 环境变量
const phone = import.meta.env.VITE_APP_PHONE
const email = import.meta.env.VITE_APP_EMAIL

// 导航状态
const navIndex = ref('')
navIndex.value = sessionStorage.getItem('navIndex') ? sessionStorage.getItem('navIndex') : 0

// 移动端菜单状态
const mobileMenuOpen = ref(false)

// 导航菜单数据
const navList = [
  {
    name: '首页',
    path: '/',
    children: []
  },
  {
    name: '公司产品',
    path: '/software',
    children: [
      {
        name: '创业者之家平台',
        path: '/software/smartTown'
      },
      {
        name: 'GuDB数据库引擎',
        path: '/software/bigData'
      },
      {
        name: '箱野盲盒商城',
        path: '/software/surpriseYard'
      }
    ]
  },
  {
    name: '相关服务',
    path: '/service',
    children: []
  },
  {
    name: '客户案例',
    path: '/cases',
    children: []
  },
  {
    name: '新闻动态',
    path: '/newsinformation',
    children: []
  },
  {
    name: '公司介绍',
    path: '/companyintroduction',
    children: []
  },
  {
    name: '工作机会',
    path: '/jobchance',
    children: []
  },
  {
    name: '联系我们',
    path: '/contactus',
    children: []
  }
]

// 获取菜单项描述
function getItemDescription(path) {
  const descriptions = {
    '/software/smartTown': '专注于创业支持的综合性平台',
    '/software/bigData': '轻量级嵌入式关系型数据库',
    '/software/surpriseYard': '一箱未知，十倍惊喜'
  }
  return descriptions[path] || ''
}

// 导航点击处理
function navClick(index, name) {
  navIndex.value = index
  sessionStorage.setItem('navIndex', index)
}

// 导航悬停处理
function handleNavHover(index) {
  // 可以在这里添加悬停效果
}

function handleNavLeave(index) {
  // 可以在这里添加离开效果
}

// 移动端菜单处理
function toggleMobileMenu() {
  mobileMenuOpen.value = !mobileMenuOpen.value
  // 防止页面滚动
  if (mobileMenuOpen.value) {
    document.body.style.overflow = 'hidden'
  } else {
    document.body.style.overflow = ''
  }
}

function closeMobileMenu() {
  mobileMenuOpen.value = false
  document.body.style.overflow = ''
}

function handleMobileNavClick(index, name) {
  navClick(index, name)
  closeMobileMenu()
}

// 组件卸载时清理
import { onUnmounted } from 'vue'
onUnmounted(() => {
  document.body.style.overflow = ''
})
</script>

<style scoped>
/* 苹果风格导航栏 */
.apple-header {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* 主导航栏 */
.main-nav {
  height: 70px;
  position: relative;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Logo */
.nav-logo {
  flex-shrink: 0;
}

.logo-link {
  display: block;
  text-decoration: none;
}

.nav-logo img {
  height: 40px;
  width: auto;
  transition: transform 0.2s ease;
}

.nav-logo img:hover {
  transform: scale(1.05);
}

/* 导航菜单 */
.nav-menu {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 0;
}

.nav-item {
  position: relative;
}

.nav-link {
  display: block;
  padding: 25px 20px;
  color: #1d1d1f;
  text-decoration: none;
  font-size: 17px;
  font-weight: 400;
  letter-spacing: -0.022em;
  transition: all 0.2s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  position: relative;
}

.nav-link:hover {
  color: #0071e3;
  text-decoration: none;
}

.nav-item.active .nav-link {
  color: #0071e3;
}

.nav-item.active .nav-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 30px;
  height: 2px;
  background: #0071e3;
  border-radius: 1px;
}

/* 下拉菜单 */
.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  min-width: 280px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-radius: 18px;
  box-shadow: 
    0 4px 20px rgba(0, 0, 0, 0.08),
    0 0 0 1px rgba(0, 0, 0, 0.05);
  opacity: 0;
  visibility: hidden;
  transform: translateX(-50%) translateY(-10px);
  transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  overflow: hidden;
  margin-top: 5px;
}

.nav-item.has-dropdown:hover .dropdown-menu {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}

.dropdown-content {
  padding: 12px 0;
}

.dropdown-item {
  display: block;
  padding: 12px 24px;
  color: #1d1d1f;
  text-decoration: none;
  transition: background-color 0.2s ease;
  border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}

.dropdown-item:last-child {
  border-bottom: none;
}

.dropdown-item:hover {
  background-color: rgba(0, 113, 227, 0.04);
  text-decoration: none;
}

.dropdown-item-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.dropdown-item-title {
  font-size: 17px;
  font-weight: 400;
  color: #1d1d1f;
}

.dropdown-item-desc {
  font-size: 14px;
  color: #86868b;
  line-height: 1.4;
}

/* 联系信息 */
.nav-contact {
  flex-shrink: 0;
  margin-left: 20px;
}

.contact-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #86868b;
  font-size: 14px;
  font-weight: 400;
}

.contact-icon {
  font-size: 16px;
}

/* 移动端菜单按钮 */
.mobile-menu-btn {
  display: none;
  background: none;
  border: none;
  padding: 8px;
  cursor: pointer;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  transition: background-color 0.2s ease;
}

.mobile-menu-btn:hover {
  background-color: rgba(0, 0, 0, 0.04);
}

.hamburger-line {
  width: 20px;
  height: 2px;
  background-color: #1d1d1f;
  transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  margin: 2px 0;
  border-radius: 1px;
}

.hamburger-line.active:nth-child(1) {
  transform: rotate(45deg) translateY(6px);
}

.hamburger-line.active:nth-child(2) {
  opacity: 0;
}

.hamburger-line.active:nth-child(3) {
  transform: rotate(-45deg) translateY(-6px);
}

/* 移动端菜单 */
.mobile-menu {
  position: fixed;
  top: 70px;
  right: -100%;
  width: 320px;
  height: calc(100vh - 70px);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  transition: right 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  z-index: 999;
  box-shadow: -4px 0 20px rgba(0, 0, 0, 0.1);
}

.mobile-menu.open {
  right: 0;
}

.mobile-menu-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
}

.mobile-nav-list {
  flex: 1;
  overflow-y: auto;
}

.mobile-nav-item {
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.mobile-nav-link {
  display: block;
  padding: 16px 24px;
  color: #1d1d1f;
  text-decoration: none;
  font-size: 17px;
  font-weight: 400;
  transition: background-color 0.2s ease;
}

.mobile-nav-link:hover {
  background-color: rgba(0, 113, 227, 0.04);
  text-decoration: none;
}

.mobile-sub-menu {
  background-color: rgba(0, 0, 0, 0.02);
  border-top: 1px solid rgba(0, 0, 0, 0.06);
}

.mobile-sub-link {
  display: block;
  padding: 12px 24px 12px 48px;
  color: #666;
  text-decoration: none;
  font-size: 15px;
  transition: background-color 0.2s ease;
  border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}

.mobile-sub-link:last-child {
  border-bottom: none;
}

.mobile-sub-link:hover {
  background-color: rgba(0, 113, 227, 0.04);
  text-decoration: none;
}

/* 移动端联系信息 */
.mobile-contact {
  padding: 20px 24px;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  background-color: rgba(0, 0, 0, 0.02);
}

.mobile-contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  color: #86868b;
  font-size: 15px;
}

/* 背景遮罩 */
.mobile-menu-overlay {
  position: fixed;
  top: 70px;
  left: 0;
  width: 100%;
  height: calc(100vh - 70px);
  background: rgba(0, 0, 0, 0.3);
  z-index: 998;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-menu,
  .nav-contact {
    display: none;
  }
  
  .mobile-menu-btn {
    display: flex;
  }
  
  .nav-container {
    padding: 0 16px;
  }
  
  .mobile-menu {
    width: 100%;
    right: -100%;
  }
  
  .mobile-menu.open {
    right: 0;
  }
}

@media (max-width: 480px) {
  .main-nav {
    height: 60px;
  }
  
  .mobile-menu {
    top: 60px;
    height: calc(100vh - 60px);
  }
  
  .mobile-menu-overlay {
    top: 60px;
    height: calc(100vh - 60px);
  }
}

/* 滚动时的效果 */
.apple-header.scrolled {
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* 暗色模式支持 */
@media (prefers-color-scheme: dark) {
  .apple-header {
    background: rgba(0, 0, 0, 0.8);
    border-bottom-color: rgba(255, 255, 255, 0.1);
  }
  
  .nav-link {
    color: #f5f5f7;
  }
  
  .nav-link:hover,
  .nav-item.active .nav-link {
    color: #0a84ff;
  }
  
  .dropdown-menu {
    background: rgba(28, 28, 30, 0.95);
    box-shadow: 
      0 4px 20px rgba(0, 0, 0, 0.3),
      0 0 0 1px rgba(255, 255, 255, 0.1);
  }
  
  .dropdown-item-title {
    color: #f5f5f7;
  }
  
  .mobile-menu {
    background: rgba(28, 28, 30, 0.95);
  }
  
  .mobile-nav-link {
    color: #f5f5f7;
  }
}
</style>

